// 栈 Stack
// 先入后出的有序列表

class ArrayStack {
    #maxSize = 0 // 栈的大小
    #stack = []  // 模拟栈 存放数据
    #top = -1

    constructor (maxSize) {
        this.#maxSize = maxSize
    }

    /**
     * 栈满
     * @return {boolean}
     */
    isFull () {
        return this.#top === this.#maxSize - 1
    }

    /**
     * 栈空
     * @return {boolean}
     */
    isEmpty () {
        return this.#top === -1
    }

    /**
     * 入栈
     * @param value
     */
    push (value) {
        if (this.isFull()) {
            console.log('栈满')
        }
        this.#top++
        this.#stack[this.#top] = value
    }

    /**
     * 出栈
     * @return {*}
     */
    pop () {
        if (this.isEmpty()) {
            console.log('栈空')
        }
        let value = this.#stack[this.#top]
        this.#top--
        return value
    }

    /**
     * 遍历
     */
    list () {
        if (this.isEmpty()) {
            console.log('栈空')
        }
        for (let i = this.#top; i >= 0; i--) {
            console.log(this.#stack[i])
        }
    }
}

function test () {
    console.log('测试-栈')
    let arrayStack = new ArrayStack(4)
    arrayStack.push(1)
    arrayStack.push(2)
    arrayStack.push(3)
    arrayStack.push(4)
    arrayStack.list()

    console.log('测试-出栈')
    arrayStack.pop()
    arrayStack.pop()
    arrayStack.list()
}

test()
